//这里是专门用来操作数据库的  但是不进行别的逻辑的编写
const { db, mongoose } = require("../utils/mongooseUtil");

const positionSchema = {
    tokenId: { type: Number, require },
    companyLogo: { type: String, required: true },
    companyName: { type: String, required: true },//required为true 表示必填选项
    positionName: { type: String, required: true },
    city: { type: String, required: true },
    salary: { type: String, required: true },
    degree: { type: String, required: true },
    type: { type: String, required: true },
    experience: { type: String, required: true },
    description: { type: String, required: true },
    createTime: { type: String, require: true },
    // createTime: { type: String }
}

const PositionModel = mongoose.model("positions", positionSchema);

const save = (data) => {
    console.log("进来了2")
    // 得到控制器传过来的 req.body 也就是前端ajax传送过来的数据
    //实例化mongoose 模型 并且保存数据
    const positionData = new PositionModel(data)
    // return positionData.save(err=>{ //进行es6 promise语法改良
    //     if(err) throw err;
    //     return "success"
    // })

    return positionData.save()
        .then(result => {
            return result
        }).catch(err => {
            return false
        })
    //关闭数据库
    // 会返回一个promise对象
}


const find = (query = { tokenId: {tokenId:""} }) => {
    query.key = query.key || query.tokenId.tokenId || {}
    return PositionModel.find(query.key).limit(query.count).skip(query.page).then(result => {
        return result
    }).catch(err => {
        return err
    })
}

//删除
const del = (tokenId) => {
    const query = { tokenId: tokenId };
    return PositionModel.deleteOne(query)
        .then((result) => {
            // 更新大于tokenId后面的全部数据
            console.log("tokenId是===" + tokenId)
            const query = { "tokenId": { $gt: tokenId } };
            const updates = { $inc: { "tokenId": -1 } };
            PositionModel.updateMany(query, updates).then(result => {
                console.log(result)
            })
            console.log("结果是", result)
            return result;
        })
}


// const del = (tokenId) => {
//     const query = {
//         tokenId: tokenId
//     }
//     return PositionModel.remove(query).then(result => {
//         console.log(tokenId)
//         let query = {"tokenId":{$gt:tokenId}}
//         let updates = {$inc:{ "tokenId": -1 }}
//         PositionModel.updateMany(query,updates).then(function(e){
//             console.log(e)
//         })
//         return result
//     }).catch(err => (false))
// }


const update = (tokenId, data) => {
    console.log(data)
    const query = { "tokenId": tokenId }
    const updates = { $set: data }
    return PositionModel.updateOne(query, updates).then(result => (result)).catch(err => (false))
}

module.exports = {
    save,
    find,
    del,
    update
}

// var conditions = {name: request.body.name};
// var updates = {$set: {name: "tiny"}};//将用户名更新为“tiny”
// userMode.update(conditions, updates, function (error) {
//     if (error) {
//         console.error(error);
//     } else {
//         console.error("更新用户名成功")
//     }
// })

// var conditions = {name: 'tiny'};
// userMode.remove(conditions, function (error) {
//     if (error) {
//         console.error(error);
//     } else {
//         console.error("用户删除成功")
//     }
// })
